package yh.thread.system.shardingtest.controller;

import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import yh.thread.system.shardingtest.service.ILongTransactionService;

import javax.annotation.Resource;
import java.util.HashMap;

/**
 * 社区接口压测汇总整理
 * @author: zyh
 * @date: 2024/12/16 11:04
 **/
@RestController
@RequestMapping("/api/v3.0/long-transaction")
public class DbConnectionController {


    @Resource
    private ILongTransactionService longTransactionService;


    /**
     *
     * 数据库连接池满测试（大事务导致），
     * 数据库配置
     * spring:
     *   datasource:
     *     druid:
     *       max-active: 5         # 设置最大连接数为5
     *       initial-size: 1       # 初始化连接数
     *       max-wait: 2000        # 获取连接的最大等待时间，单位毫秒
     *       min-idle: 1           # 最小空闲连接数
     *
     * //后续请求进来均失败报如下错误
     * 报错记录：
     * 2024-12-16 11:48:03.811 [http-nio-8096-exec-44] ERROR o.a.c.c.C.[.[.[.[dispatcherServlet] -
     * Servlet.service() for servlet [dispatcherServlet] in context with path []
     * threw exception [Request processing failed;
     * nested exception is org.springframework.transaction.CannotCreateTransactionException:
     * Could not open JDBC Connection for transaction;
     * nested exception is com.alibaba.druid.pool.GetConnectionTimeoutException:
     * wait millis 30005, active 2, maxActive 2, creating 0] with root cause
     *
     * @return
     */
    @GetMapping("exec-test")
    public HashMap<String,Object> executeLongTransaction(){
        longTransactionService.executeLongTransaction();
        return new HashMap<>();
    }
}
